<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自有线路</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../js/element-ui-2.13.0/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/index.css"/>
    <link rel="stylesheet" href="../../styles/icon/iconfont.css"/>
    <link rel="stylesheet" href="../../styles/pages.css"/>
</head>
<body>
<div class="dashboard-container" id="category-app">
    <div class="container">
        <div
                class="tableBar"
                style="display: inline-block"
        >
            <el-input
                    v-model="name"
                    placeholder="请输入姓名(按Enter查询)"
                    style="width: 250px"
                    clearable
                    @keyup.enter.native="handleQuery"
            >
            </el-input>
            <el-input
                    v-model="phone"
                    placeholder="请输入手机号"
                    style="width: 250px"
                    clearable
                    @keyup.enter.native="handleQuery"
            >
            </el-input>
            <el-input
                    v-model="wechat"
                    placeholder="请输入微信号"
                    style="width: 250px"
                    clearable
                    @keyup.enter.native="handleQuery"
            >
            </el-input>
            <el-select v-model="type" placeholder="请选择用户类型" @change="handleQuery" clearable>
                <el-option
                        v-for="item in userTypes"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>
            <el-button
                    type="primary"
                    @click="addUser('user')"
                    v-if="userType < 2"
            >
                + 新增用户
            </el-button>
        </div>
        <el-table
                :data="tableData"
                stripe
                class="tableBox"
                @sort-change="handleSortChange"
        >
            <el-table-column
                    prop="realName"
                    label="姓名"
                    sortable="true"
            />
            </el-table-column>
            <el-table-column
                    prop="username"
                    label="用户名"
                    sortable="true"
            />
            </el-table-column>
            <el-table-column
                    prop="phone"
                    label="手机号"
                    sortable="true"
            />
            </el-table-column>
            <el-table-column
                    prop="wechat"
                    label="微信号"
                    sortable="true"
            />
            </el-table-column>
            <el-table-column
                    prop="userType"
                    label="用户类型"
                    sortable="true"
            />
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="160"
                    align="center"
            >
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            size="small"
                            class="blueBug"
                            @click="editHandle(scope.row)"
                            v-if="scope.row.type != 0"
                    >
                        修改
                    </el-button>
                    <el-button
                            type="text"
                            size="small"
                            class="delBut non"
                            @click="deleteHandle(scope.row)"
                            v-if="scope.row.type != 0"
                            :disabled="userType != 0"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
    <el-dialog
            :title="userParams.title"
            :visible.sync="userParams.dialogVisible"
            width="30%"
            :before-close="handleClose"
    >
        <el-form
                class="demo-form-inline"
                label-width="100px"
                :model="userParams"
                ref="userParams"
        >
            <el-form-item
                    label="姓名："
                    prop="realName"
                    :rules="[
                      { required: true, message: '姓名不能为空', trigger: 'blur' }]"
            >
                <el-input
                        v-model="userParams.realName"
                        placeholder="请输入姓名"
                        maxlength="14"
                        clearable
                />
            </el-form-item>
            <el-form-item
                    label="用户名："
                    prop="username"
                    :rules="[
                      { required: true, message: '用户名不能为空', trigger: 'blur' }]">
                <el-input
                        v-model="userParams.username"
                        placeholder="请输入用户名"
                        maxlength="14"
                        clearable
                />
            </el-form-item>
            <el-form-item
                    label="密码："
                    prop="password"
                    :rules="[
                      { required: true, message: '密码不能为空', trigger: 'blur' }]">
                <el-input
                        v-model="userParams.password"
                        placeholder="请输入密码"
                        maxlength="14"
                        clearable
                />
            </el-form-item>
            <el-form-item
                    label="手机号："
                    prop="phone"
            >
                <el-input
                        v-model="userParams.phone"
                        placeholder="请输入手机号"
                        maxlength="14"
                        clearable
                />
            </el-form-item>
            <el-form-item label="微信号："
                          prop="wechat"
            >
                <el-input
                        v-model="userParams.wechat"
                        placeholder="请输入微信号"
                        maxlength="14"
                        clearable
                />
            </el-form-item>
            <el-form-item
                    label="用户类型："
                    prop="type"
                    :rules="[
                      { required: true, message: '用户类型不能为空', trigger: 'blur' }]">
                <el-select v-model="userParams.type"
                           placeholder="请选择用户类型"
                           @change="handleQuery"
                           clearable
                           :disabled="userType > 1">
                    <el-option
                            v-for="item in userTypes"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span
                slot="footer"
                class="dialog-footer"
        >
        <el-button
                size="medium"
                @click="handleClose()"
        >取 消</el-button>
        <el-button
                type="primary"
                size="medium"
                @click="submitForm()"
        >确 定</el-button>
        <el-button
                v-if="action != 'edit'"
                type="primary"
                size="medium"
                class="continue"
                @click="submitForm('go')"
        > 保存并继续添加 </el-button>
      </span>
    </el-dialog>
</div>
<script src="../../js/vue.js"></script>
<script src="../../js/element-ui-2.13.0/lib/index.js"></script>
<script src="../../js/axios-0.18.0.js"></script>
<script src="../../js/request.js"></script>
<script src="../../js/query.js"></script>
<script>
    new Vue({
        el: '#category-app',
        data() {
            return {
                realName: '',
                name: '',
                type: '',
                phone: '',
                wechat: '',
                userType: '',
                action: '',
                counts: 0,
                pageNum: 1,
                pageSize: 10,
                tableData: [],
                orderByColumn: '',
                isAsc: '',
                userTypes: [
                    {
                        label: "普通用户",
                        value: 2
                    },
                    {
                        label: "技术运维",
                        value: 1
                    }
                ],
                userParams: {
                    'title': '添加自有线路',
                    'dialogVisible': false,
                    'id': '',
                    'realName': '',
                    'username': '',
                    'password': '',
                    'type': '',
                    'phone': '',
                    'wechat': ''
                }
            }
        },
        computed: {},
        created() {
            this.dealCookie();
            this.init();
        },
        mounted() {
        },
        methods: {
            dealCookie() {
                // 对cookie操作
                var cookies = document.cookie;
                var cookie_array = cookies.split(";");
                for (var i = 0; i < cookie_array.length; i++) {
                    var cookie = cookie_array[i];
                    var array = cookie.split("=");
                    if (array[0] == "realName") {
                        this.realName = array[1];
                    }
                    if (array[0] == " userType") {
                        this.userType = array[1];
                    }
                }
            },
            async init() {
                await queryAllUser({
                    'pageNum': this.pageNum,
                    'pageSize': this.pageSize,
                    'property': this.property,
                    'orderByColumn': this.orderByColumn,
                    'isAsc': this.isAsc,
                    'realName': this.name,
                    'type': this.type,
                    'phone': this.phone,
                    'wechat': this.wechat
                }).then(res => {
                    if (String(res.code) == 200) {
                        this.tableData = res.result.data
                        this.counts = Number(res.result.total)
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            },
            handleQuery() {
                this.pageNum = 1;
                this.init();
            },
            // 添加
            addUser(st) {
                if (st == 'user') {
                    this.userParams.title = '新增用户'
                }
                this.action = 'add'
                this.userParams.id = ''
                this.userParams.realName = ''
                this.userParams.username = ''
                this.userParams.password = ''
                this.userParams.type = ''
                this.userParams.phone = ''
                this.userParams.wechat = ''
                this.userParams.dialogVisible = true
            },
            editHandle(dat) {
                this.userParams.title = '修改用户信息'
                this.action = 'edit'
                this.userParams.id = dat.id
                this.userParams.realName = dat.realName
                this.userParams.username = dat.username
                this.userParams.password = dat.password
                this.userParams.type = dat.type
                this.userParams.phone = dat.phone
                this.userParams.wechat = dat.wechat
                this.userParams.dialogVisible = true
            },
            // 关闭弹窗
            handleClose(st) {
                this.userParams.dialogVisible = false
                this.$refs[`userParams`].resetFields()
            },
            //删除
            deleteHandle(dat) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    deleteUser({
                        'id': dat.id,
                        'username': dat.username
                    }).then(res => {
                        if (res.code === 200) {
                            this.$message.success('删除成功！')
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            //数据提交
            submitForm(st) {
                this.$refs[`userParams`].validate(valid => {
                    if (valid) {

                        const userParams = this.userParams
                        if (this.action === 'add') {
                            addUser(userParams).then(res => {
                                if (res.code === 200) {
                                    this.$message.success('用户添加成功！')
                                    if (!st) {
                                        this.userParams.dialogVisible = false
                                    }
                                    this.handleQuery()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        } else if (this.action === 'edit') {
                            updateUser(userParams).then(res => {
                                console.log(res)
                                if (res.code === 200) {
                                    this.$message.success('用户修改成功！')
                                    if (!st) {
                                        this.userParams.dialogVisible = false
                                    }
                                    this.handleQuery()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }
                        // } else if(valid) {
                        //     const reg = /^\d+$/
                        //     if (reg.test(this.classData.sort)) {
                        //         editCategory({
                        //             'id': this.classData.id,
                        //             'name': this.classData.name,
                        //             sort: this.classData.sort
                        //         }).then(res => {
                        //             if (res.code === 1) {
                        //                 this.$message.success('分类修改成功！')
                        //                 this.classData.dialogVisible = false
                        //                 this.handleQuery()
                        //             } else {
                        //                 this.$message.error(res.msg || '操作失败')
                        //             }
                        //         }).catch(err => {
                        //             this.$message.error('请求出错了：' + err)
                        //         })
                        //     } else {
                        //         this.$message.error('排序只能输入数字类型')
                        //     }
                        // } else {
                        //     this.$message.error('请输入分类名称或排序')
                        // }

                    }
                })
            }
            ,
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.pageNum = val
                this.init()
            },
            handleSortChange(val) {
                if (val.column) {
                    this.orderByColumn = val.prop
                    this.isAsc = val.order == 'ascending' ? 'ASC' : 'DESC'
                }
                this.init()
            }
        }
    })
</script>
</body>
</html>